var saveDHC = require("./../dhc/saveDHC");

//底部操作
var exports = {};

var product = null;

var panel = $(".appearance-button");
var sizePanel = $(".size-button");
var facePanel = $(".face-button");

var buyPanel = $(".add-to-cart-sizes");

exports.bind = function($product) {
	product = $product;
	
	$(".addCartDiv").on("click", function(){
		$(".add-to-cart-mask").show();
	});
	
	$("#closeCart").on("click", function(){
		$(".add-to-cart-mask").hide();
	});
	
	$(".add-to-cart-btn").on("click", function(){
		layer.msg("等待加购接口");
	});
	
	$(".shoppingCartDiv").on("click", function(){
		saveDHC.doSave();
	});
	
	exports.update();
}

exports.update = function(){
	//面
	var faces = product.faces;
	facePanel.find(".face-list").empty();
	
	for (var i = 0; i < faces.length; i++) {
		var data = faces.getValueAt(i);
		var li = $('<li></li>');
		li.attr("id", data.id);
		li.attr("data-id", data.id);
		li.attr("data-name", data.name);
		li.text(data.name);
		facePanel.find(".face-list").append(li);
	}
	
	facePanel.find(".face-list").find("li").on("click", function(){
		facePanel.find(".face-list").find("li").removeClass("cur");
		$(this).addClass("cur");
		var id = $(this).data("id");
		setFace(id);
	})
	
	var currFace = product.currFace;
	if(currFace && currFace.id){
		facePanel.find(".face-list").find("#"+currFace.id).click();
	}else{
		facePanel.find(".face-list").find("li").eq(0).click();
	}
	
	//颜色
	var colors = product.colors;
	panel.find(".color-picker-list").empty();
	
	for (var i = 0; i < colors.length; i++) {
		var data = colors[i];
		if(!data.id){
			data.id = Design.utils.uuid();
		}
		var li = $('<li><a></a></li>');
		li.attr("id", data.id);
		li.attr("data-num", i);
		li.attr("data-cname", data.color);
		li.attr("data-c", data.image);
		li.css("display", "inline-table");
		li.find("a").css("background-color", data.image);
		panel.find(".color-picker-list").append(li);
	}
	
	panel.find(".color-picker-list").find("li").on("click", function(){
		panel.find(".color-picker-list").find("li").removeClass("cur");
		$(this).addClass("cur");
		var num = parseInt($(this).data("num"));
		var colorobj = colors[num];
		setColor(colorobj);
	})
	
	var currColor = product.currColor;
	if(currColor && currColor.id){
		var id = currColor.id;
		panel.find(".color-picker-list").find("#"+id).click();
	}else{
		panel.find(".color-picker-list").find("li").eq(0).click();
	}
	
	//大小
	var sizes = product.sizes;
	sizePanel.find(".size-picker-list").empty();
	
	for (var i = 0; i < sizes.length; i++) {
		var data = sizes[i];
		if(!data.id){
			data.id = Design.utils.uuid();
		}
		var li = $('<li></li>');
		li.attr("id", data.id);
		li.attr("data-num", i);
		li.attr("data-name", data.name);
		li.text(data.name);
		sizePanel.find(".size-picker-list").append(li);
	}
	
	sizePanel.find(".size-picker-list").find("li").on("click", function(){
		sizePanel.find(".size-picker-list").find("li").removeClass("cur");
		$(this).addClass("cur");
		var num = parseInt($(this).data("num"));
		var sizeobj = sizes[num];
		setSize(sizeobj);
	})
	
	var currSize = product.currSize;
	if(currSize && currSize.id){
		var id = currSize.id;
		sizePanel.find(".size-picker-list").find("#"+id).click();
	}else{
		sizePanel.find(".size-picker-list").find("li").eq(0).click();
	}
	
	//购物车
	buyPanel.find("ul").empty();
	
	for (var i = 0; i < sizes.length; i++) {
		var data = sizes[i];
		var li = $('<li class="letter"></li>');
		li.attr("data-num", i);
		li.attr("data-name", data.name);
		li.attr("data-price", data.price);
		li.append('<div class="add-to-cart-name fb">'+data.name+'</div>');
		li.append('<div class="add-to-cart-price">￥'+data.price+'</div>');
		li.append('<div class="add-to-cart-qty fr"><i class="mul not">-</i><input type="text" maxlength="5" value="0"><i class="add">+</i></div>');
		buyPanel.find("ul").append(li);
	}
	
	buyPanel.find(".mul").on("click", function(){
		var isnot = $(this).hasClass("not");
		if(isnot) return;
		var input = $(this).parent().find("input");
		var num = parseInt(input.val());
		num--;
		input.val(num);
		
		if(num == 0){
			$(this).addClass("not");
		}else{
			$(this).removeClass("not");
		}
		
		countPrice();
	});
	
	buyPanel.find(".add").on("click", function(){
		var isnot = $(this).hasClass("not");
		if(isnot) return;
		var input = $(this).parent().find("input");
		var num = parseInt(input.val());
		num++;
		input.val(num);
		
		var muli = $(this).parent().find(".mul");
		if(num == 0){
			muli.addClass("not");
		}else{
			muli.removeClass("not");
		}
		
		countPrice();
	});
	
	/* 计算价格 */
	function countPrice(){
		var list = buyPanel.find("ul li");
		var totalNum = 0;
		var totalPrice = 0;
		for(var i=0; i<list.length; i++){
			var tmp = list.eq(i);
			var tmpnum = parseInt(tmp.find("input").val());
			var tmpprice = tmp.data("price");
			totalNum += tmpnum;
			totalPrice += tmpnum*tmpprice;
			totalPrice = Math.round(totalPrice*100)/100;
		}
		var avg = Math.round(totalPrice/totalNum*100) / 100;
		if(isNaN(avg)){
			avg = 0;
		}
		$(".add-to-cart-total span").text(totalPrice);
		$(".add-to-cart-num span").text(totalNum);
		$(".add-to-cart-ave-price span").text(avg);
	}
}

function setColor(obj){
	if(product){
		product.setColor(obj);
	}
}

function setSize(obj){
	if(product){
		product.setSize(obj);
		$(".price-div").text("￥"+ obj.price);
	}
}

function setFace(id){
	if(product){
		product.activeFace(id);
	}
}

exports.unbind = function($product) {
	product = $product;
}

module.exports = exports;